//
//  StatusIndicator.swift
//  TestScroll
//
//  Created by kkkelicheng on 2021/12/6.
//

import UIKit

///自定义的StatusIndicator需要符合这个协议。
/*
 具体例子看StatusIndicator_Base的实现
 点击了image触发 statusAction。
 @objc func imageTap(){
     statusAction?()
 }
 */
protocol StatusActionRequired : AnyObject{
    var statusAction : (()->Void)? {set get}
}

typealias StatusActionRequiredView = UIView & StatusActionRequired

enum ViewDataStatus {
    
    /// 没有状态，隐藏
    case none
    /// 正在loding
    case loading
    /// 没有数据
    case dataEmpty
    /// 网络异常
    case badNetwork
    /// 展示其他的错误
    case showError(String)
    /// 展示其他的view,设置自己的触发，可以不调用statusAction
    case showCustomView(StatusActionRequiredView)

    
    func statusView() -> StatusActionRequiredView? {
        switch self {
        case .dataEmpty:
            return StatusIndicator_Empty()
        case .badNetwork:
            return StatusIndicator_BadNetwork()
        case .showError(let string):
            return StatusIndicator_ShowError.init(msg: string)
        case .showCustomView(let statusActionRequiredView):
            return statusActionRequiredView
        case .loading:
            return StatusIndicator_Loading()
        default:
            return nil
        }
    }
}

struct StatusIndicatorAssociatedKeys {
    static var viewStatusKey: String = "viewStatusKey"
}

///StatusIndicator处理的相关操作
protocol StatusIndicatorDelegate : AnyObject {
    ///点击了status上的相关按钮
    func statusIndicatorClickAction(_ indicator : StatusIndicator)
}

extension StatusIndicatorDelegate {
    func statusIndicatorClickAction(_ indicator : StatusIndicator){}
}


class StatusIndicator {
    //关联的需要有状态的View
    weak var view : UIView?
    //默认的状态
    var status : ViewDataStatus = .none {
        didSet {
            changeIndicatorStatus(status)
        }
    }
    //展示状态的view
    private var statusView : StatusActionRequiredView?

    //控制代理
    weak var delegate : StatusIndicatorDelegate?
    
    func changeIndicatorStatus(_ status:ViewDataStatus) {
        statusView?.removeFromSuperview()
        let newStatusView = status.statusView()
        if let view = view , let newStatusView = newStatusView{
            view.addSubview(newStatusView)
            newStatusView.frame = view.bounds
            newStatusView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
            newStatusView.statusAction = { [weak self] in
                self?.commonClickAction()
            }
            view.bringSubviewToFront(newStatusView)
            statusView = newStatusView
        }
    }
    
    func commonClickAction(){
        self.delegate?.statusIndicatorClickAction(self)
    }
    
    deinit {
        print("StatusIndicator deinit")
    }
}


extension UIView {
    
    var statusIndicator : StatusIndicator? {
        get {
            var indicator = objc_getAssociatedObject(self, &StatusIndicatorAssociatedKeys.viewStatusKey) as? StatusIndicator
            if indicator == nil {
                indicator = StatusIndicator()
                indicator!.view = self
                 objc_setAssociatedObject(self, &StatusIndicatorAssociatedKeys.viewStatusKey, indicator, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
            return indicator
        }
    }
}

